﻿//Write a program to convert binary numbers to their
//decimal representation.

using System;

class BinToDec
{
    static int ToDec(string S)
    {
        char[] digit = S.ToCharArray(0, S.Length);
        int result = 0;
        if (S.Length == 32)
        {
            if (digit[0] == 1)
            {
                for (int i = 1; i < S.Length; i++)
                {
                    if (digit[i] == '1')
                    {
                        result = result + (int)Math.Pow(2, S.Length - i);
                    }
                }
                result = result * (-1);
            }
            else
            {
                for (int i = 0; i < S.Length; i++)
                {
                    if (digit[i] == '1')
                    {
                        result = result + (int)Math.Pow(2, S.Length - i - 1);
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < S.Length; i++)
            {
                if (digit[i] == '1')
                {
                    result = result + (int)Math.Pow(2, S.Length - i - 1);
                }
            }
        }
        return result;
    }

    static void Main()
    {
        //string a = "01111111111111111111111111111111"; // for 2 147 483 647 (max)
        //string a = "11111111111111111111111111111111"; // For -1
        //string a = "10000000000000000000000000000001"; // For -2 147 483 648 (-max)
        Console.WriteLine("Enter number in binary format:");
        string a = Console.ReadLine();
        int b = ToDec(a);
        Console.WriteLine();
        Console.WriteLine("The number in decimal is:");
        Console.WriteLine(b);
    }
}

